import csv
import json
import os

import pandas as pd

# Define the folder containing the JSON files
folder_path = "./"  # Current folder

# List to hold all data
all_data = []


def save_csv(year_data, year):
    df = pd.json_normalize(year_data)
    csv_file_path = f"./{year}.csv"
    df.to_csv(
        csv_file_path,
        index=False,
        encoding="gbk",
        quoting=csv.QUOTE_NONNUMERIC,
    )
    print(f"Data has been successfully merged into {csv_file_path}")


# Loop through all files in the folder
for filename in os.listdir(folder_path):
    if filename.endswith(".json"):
        file_path = os.path.join(folder_path, filename)
        with open(file_path, "r", encoding="utf-8-sig") as json_file:
            all_data = json.load(json_file)

for year in all_data.keys():
    print(f"year: {year}")
    year_data = []
    for data in all_data[year]:
        for stock, details in data.items():
            print(f"index: {stock} ")
            print(f"details: {details}")
            stock_code = stock.split("-")[0]
            stock = stock.split("-")[1]
            company_name = stock.split("20")[0]
            stock_info = {
                "stock_code": stock_code,
                "company_name": company_name,
                "report_year": year,
                "big_data": 1 if details["big_data"] else 0,
                "artificial_intelligence": 1
                if details["artificial_intelligence"]
                else 0,
                "mobile_internet": 1 if details["mobile_internet"] else 0,
                "cloud_computing": 1 if details["cloud_computing"] else 0,
                "Internet_of_Things": 1 if details["Internet_of_Things"] else 0,
                "blockchain_technology": 1 if details["blockchain_technology"] else 0,
                "digital_transformation": 1
                if details["big_data"]
                or details["artificial_intelligence"]
                or details["mobile_internet"]
                or details["cloud_computing"]
                or details["Internet_of_Things"]
                or details["blockchain_technology"]
                else 0,
            }
            year_data.append(stock_info)
    save_csv(year_data, year)
